<?php
namespace app\store\controller\transform;

use app\store\controller\Controller;
use app\store\model\QaType;
use \think\Db;
use think\Cache;
/**
 * 问答系统
 * Class User
 * @package app\store\controller
 */
class Qa extends Controller
{
    /**
     * 试卷列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function boxquestion()
    {
        //mmmodify
        $map = ['wxapp_id'=>$this->wxapp_id];
        $list = Db::name('qa_boxquestion')
            ->where($map)
            ->order('bq_id desc')
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
        $this->assign('list',$list);
        return $this->fetch('boxquestion');
    }


    public function add()
    {
        $model = new QaType;
        if (!$this->request->isAjax()) {
            $list = $model->getCacheTree();
            $this->assign('list',$list);
            return $this->fetch('add');
        }
        
        // 新增记录
        $category = $this->postData('category');
        $category['wxapp_id'] = $model::$wxapp_id;  //添加wxappid
        $category['create_time'] = date('Y-m-d H:i:s',time());

        $type = Db::name('qa_type')->where('qt_id',$category['bq_type2'])->find();
        if($type['qt_pid']>0){
            $category['bq_type1'] = $type['qt_pid'];
        }

        if (Db::name('qa_boxquestion')->insert($category)) {
            return $this->renderSuccess('添加成功', url('transform.qa/boxquestion'));
        }
        return $this->renderError('添加失败');
    }


    public function edit($bq_id){
        $model = new QaType;
        if (!$this->request->isAjax()) {
            $list = $model->getCacheTree();
            $info = Db::name('qa_boxquestion')->where('bq_id',$bq_id)->find();


            $this->assign('list',$list);
            $this->assign('info',$info);
            return $this->fetch('edit');
        }
        
        // 新增记录
        $category = $this->postData('category');
        $category['update_time'] = date('Y-m-d H:i:s',time());

        $type = Db::name('qa_type')->where('qt_id',$category['bq_type2'])->find();
        if($type['qt_pid']>0){
            $category['bq_type1'] = $type['qt_pid'];
        }

        if (Db::name('qa_boxquestion')->where('bq_id',$bq_id)->update($category) !==false) {

            return $this->renderSuccess('修改成功', url('transform.qa/boxquestion'));
        }
        return $this->renderError('修改失败');
    }


    public function delete($bq_id)
    {
        if (Db::name('qa_boxquestion')->where('bq_id',$bq_id)->delete()) {
            return $this->renderSuccess('删除成功', url('transform.qa/boxquestion'));
        }

        return $this->renderSuccess('删除成功');
    }


    public function perquestion($pq_pid)
    {
        $map['pq_pid'] = $pq_pid;
        $list = Db::name('qa_perquestion')
            ->where($map)
            ->order('pq_sort desc,pq_id desc')
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
        $boxquestion = Db::name('qa_boxquestion')->where('bq_id',$pq_pid)->find();
        $this->assign('list',$list);
        $this->assign('boxquestion',$boxquestion);
        return $this->fetch('perquestion');
    }
	
    public function addperquestion()
    {
        $model = new QaType;
        if (!$this->request->isAjax()) {
            return $this->fetch('addperquestion');
        }

        $category = $this->postData('category');
        if(!isset($category['pq_pid'])){
            return $this->renderError('pq_pid参数错误');
        }

        $boxquestion = Db::name('qa_boxquestion')->where('bq_id',$category['pq_pid'])->find();
        if(!$boxquestion){
            return $this->renderError('所属试卷错误！');
        }

        $category['pq_type1'] = $boxquestion['bq_type1'];
        $category['pq_typename1'] = $boxquestion['bq_typename1'];
        $category['pq_type2'] = $boxquestion['bq_type2'];
        $category['pq_typename2'] = $boxquestion['bq_typename2'];
        $category['wxapp_id'] = $model::$wxapp_id;
        $category['create_time'] = date('Y-m-d H:i:s');
        $category['update_time'] = date('Y-m-d H:i:s');
        
        // 新增记录
        
        if (Db::name('qa_perquestion')->insert($category)) {
            return $this->renderSuccess('添加成功', url('transform.qa/perquestion',['pq_pid'=>$category['pq_pid']]));
        }
        return $this->renderError('添加失败');
    }


    public function editperquestion($pq_id){
        $model = new QaType;
        if (!$this->request->isAjax()) {
            $info = Db::name('qa_perquestion')->where('pq_id',$pq_id)->find();
            $this->assign('info',$info);
            return $this->fetch('editperquestion');
        }

        // 新增记录
        $category = $this->postData('category');

        if(!isset($category['pq_pid'])){
            return $this->renderError('pq_pid参数错误');
        }

        $boxquestion = Db::name('qa_boxquestion')->where('bq_id',$category['pq_pid'])->find();
        if(!$boxquestion){
            return $this->renderError('所属试卷错误！');
        }

        $category['pq_type1'] = $boxquestion['bq_type1'];
        $category['pq_typename1'] = $boxquestion['bq_typename1'];
        $category['pq_type2'] = $boxquestion['bq_type2'];
        $category['pq_typename2'] = $boxquestion['bq_typename2'];

        $category['update_time'] = date('Y-m-d H:i:s');
        if (Db::name('qa_perquestion')->where('pq_id',$pq_id)->update($category) !==false) {

            return $this->renderSuccess('添加成功', url('transform.qa/perquestion',['pq_pid'=>$category['pq_pid']]));
        }
        return $this->renderError('修改失败');
    }


    public function perquestiondelete($pq_id)
    {
        $info = Db::name('qa_perquestion')->where('pq_id',$pq_id)->find();
        if (Db::name('qa_perquestion')->where('pq_id',$pq_id)->delete()) {

            return $this->renderSuccess('删除成功', url('transform.qa/perquestion',['pq_pid'=>$info['pq_pid']]));
        }

        return $this->renderSuccess('删除成功');
    }
    

	public function type(){
        $model = new QaType;
        $list = $model->getCacheTree();
        $this->assign('list',$list);
		return $this->fetch('qa_type');
	}
	
    public function typeadd()
    {
        $model = new QaType;
        if (!$this->request->isAjax()) {
            $list = $model->getCacheTree();
            $this->assign('list',$list);
            return $this->fetch('typeadd');
        }

        // 新增记录
        $category = $this->postData('category');
        $category['wxapp_id'] = $model::$wxapp_id;  //添加wxappid

        if ($model->allowField(true)->save($category)) {
            
            return $this->renderSuccess('添加成功', url('transform.qa/type'));
        }
        return $this->renderError('添加失败');
    }


    public function typeedit($qt_id){
        $model = new QaType;
        if (!$this->request->isAjax()) {
            $list = $model->getCacheTree();
            $info = Db::name('QaType')->where('qt_id',$qt_id)->find();


            $this->assign('list',$list);
            $this->assign('info',$info);
            return $this->fetch('typeedit');
        }
        
        // 新增记录
        $category = $this->postData('category');

        if (Db::name('QaType')->where('qt_id',$qt_id)->update($category) !==false) {

            return $this->renderSuccess('修改成功', url('transform.qa/type'));
        }
        return $this->renderError('修改失败');
    }

    /**
     * 删除商品分类
     * @param $category_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function typedelete($qt_id)
    {
        if (Db::name('QaType')->where('qt_id',$qt_id)->update(['qt_delete'=>1])) {

            return $this->renderSuccess('删除成功', url('transform.qa/type'));
        }

        return $this->renderSuccess('删除成功');
    }

}
